<!-- 出账关联报名信息 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="1300px"
    @close="reset"
  >
    <div class="relation-container">
      <!-- 左侧树形菜单 -->
      <div class="tree-panel">
        <splitpanes
          :horizontal="this.$store.getters.device === 'mobile'"
          class="default-theme"
          style="height: 100vh"
        >
          <pane size="10">
            <div class="head-container">
              <el-input
                v-model="projectName"
                placeholder="请输入培训项目"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px; width: 70%"
              />
            </div>
            <div class="head-container">
              <el-tree
                :data="projectOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                node-key="id"
                default-expand-all
                highlight-current
                @node-click="handleNodeClick"
              />
            </div>
          </pane>
        </splitpanes>
      </div>

      <!-- 右侧内容区 -->
      <div class="content-panel">
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="姓名">
            <el-input
              v-model="searchForm.studentName"
              placeholder="请输入姓名"
            />
          </el-form-item>
          <el-form-item label="证件编号">
            <el-input
              v-model="searchForm.idNumber"
              placeholder="请输入证件编号"
            />
          </el-form-item>
          <el-form-item label="单位名称">
            <el-input
              v-model="searchForm.unitName"
              placeholder="请输入单位名称"
            />
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input
              v-model="searchForm.phonenumber"
              placeholder="请输入手机号码"
            />
          </el-form-item>
          <el-form-item label="费用金额">
            <el-input
              v-model="searchForm.projectSum"
              placeholder="请输入费用金额"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="fetchData">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 数据表格 -->
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="studentName" label="姓名" />
          <el-table-column prop="idNumber" label="证件编号" />
          <el-table-column prop="phonenumber" label="电话" />
          <el-table-column prop="unitName" label="单位名称" />
          <el-table-column prop="contact" label="联系人" />
          <el-table-column prop="unitPhonenumber" label="联系人电话" />
          <el-table-column prop="projectSum" label="费用" />
        </el-table>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        />
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { projectTreeSelect } from "@/api/training/projects";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";
import { registrationList } from "@/api/training/registration";

export default {
  name: "WithdrawalRelationDialog",
  components: { Splitpanes, Pane },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "出账关联报名信息",
    },
  },
  data() {
    return {
      searchForm: {
        studentName: "",
        idNumber: "",
        unitName: "",
        phonenumber: "",
        projectSum: "",
      },

      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      selectedRows: [],
      //树形结构数据
      // 所有部门树选项
      projectOptions: null,
      projectName: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: undefined,
      },
    };
  },
  created() {
    this.getProjectTree();
  },
  methods: {
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.projectId = data.id;
      this.queryParams.pageNum = 1;
      //查询人员list
      registrationList(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },
    // 树节点过滤方法
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 获取树形数据
    getProjectTree(val) {
      var params = { projectName: val };
      params.projectId = this.queryParams.projectId;
      projectTreeSelect(params).then((response) => {
        this.projectOptions = response.data;
        this.enabledProjectOptions = this.filterDisabledDept(
          JSON.parse(JSON.stringify(response.data))
        );
      });
    },
    reset() {
      // 重置搜索表单
      this.searchForm = {
        studentName: "",
        idNumber: "",
        unitName: "",
        phonenumber: "",
        projectSum: "",
      };
      // 重置表格数据和选择项
      this.tableData = [];
      this.selectedRows = [];
      // 重置分页信息
      this.currentPage = 1;
      this.pageSize = 10;
      this.total = 0;
      // 重置查询参数
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        projectId: undefined,
      };
      // 重置项目树搜索
      this.projectName = "";
      // 清除树节点选中状态
      if (this.$refs.tree) {
        this.$refs.tree.setCurrentKey(null);
      }
      // 关闭组件
      this.$emit("update:visible", false);
      this.$emit("close", false);
    },
    resetSearch() {
      this.searchForm = {};
      // 重置树节点选择状态
      this.queryParams.projectId = undefined;
      if (this.$refs.tree) {
        this.$refs.tree.setCurrentKey(null);
      }
      this.fetchData();
    },
    fetchData() {
      registrationList(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },

    handleSelectionChange(val) {
      this.selectedRows = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    confirm() {
      this.$emit("confirm", this.selectedRows);
      this.$emit("update:visible", false);
    },
  },
  // mounted() {
  //   this.fetchData();
  // },
  watch: {
    // 根据名称筛选部门树
    projectName(val) {
      this.$refs.tree.filter(val);
      this.getProjectTree(val);
    },
    // 监听visible变化，当对话框打开时重新加载数据
    visible(newVal) {
      if (newVal) {
        // 延迟执行确保DOM已更新
        this.$nextTick(() => {
          this.getProjectTree();
          this.fetchData();
        });
      }
    },
  },
};
</script>

<style scoped>
.relation-container {
  display: flex;
  height: 600px;
}
.tree-panel {
  width: 300px;
  border-right: 1px solid #ebeef5;
  overflow-y: auto;
}
.content-panel {
  flex: 1;
  padding: 20px;
}
</style>